﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace CRUD
{
    public class DS_NET: IDS
    { 
        public static TcpClient clientSocket = new TcpClient();
        public static NetworkStream serverStream = default(NetworkStream);  
        public DS_NET()
        {
            if (clientSocket.Connected == false)
            {
                clientSocket.Connect("127.0.0.1", 4545);
            }
            serverStream = clientSocket.GetStream();
        }

        public override void Create(Person pp)
        {
            string strArray = "";
            strArray += pp.SaveToSCV();
            strArray += ";";
            string message = "Create;" + path + "*" + strArray;
            connect(message);         
           
        }
        public override MyList Read()
        {
            MyList x = new MyList();
            string message = "Read;"+path;
            connect(message); 
            while (true)
            {
                if (serverStream.DataAvailable)
                {
                    int i = clientSocket.Available;
                    byte[] bytes = new byte[i];
                    //ThisSocket.Receive(bytes);
                    //stream.Read(bytes, 0, i);
                    //msg = Encoding.Default.GetString(bytes);

                    serverStream.Read(bytes, 0, i);
                    message = Encoding.Default.GetString(bytes);
                    if (message == "null")
                    {
                        x = null;
                    }
                    else
                    {
                        string[] split = message.Split(new Char[] { '<' });
                        foreach (string str in split)
                        {
                            if (str != "")
                            {
                                string[] spliP = str.Split(new Char[] { ';' });
                                Person type = new Person();
                                type.LoadFromCSV(spliP);
                                x.Add(type);
                            }
                        }
                    }
                    return x;
                }

            }           
        }
        public override void Update(Person pp)
        {
            string message = "Update" + path + "*" + pp.SaveToSCV();
            connect(message);
        }      
        
        public override IDS IsReady(string Fname)
        {
            IDS ret = null;
            string[] strArray = Fname.Split('*');            
            string extension = strArray[0];
            if (extension == "NET")
            {
                path = strArray[1];
                ret = this;               
            }
            else
            {
                if (!(next == null)) ret = next.IsReady(Fname);
            }
            return ret;
        } 
        public override void Delete(int id)
        {
            string message = "Delete" + path +"*"+id.ToString();
            connect(message);
        }

        void connect(string SendMSG)
        {
            byte[] outStream = System.Text.Encoding.Default.GetBytes(SendMSG);
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();
          
        }
    }
}
